package com.qingke.pms.command.user;

import java.util.Date;

import com.qingke.pms.command.UserCommand;
import com.qingke.pms.pojo.Project;
import com.qingke.pms.pojo.User;
import com.qingke.pms.util.PMSConsole;

public class AddProjectCommand extends UserCommand {

	public AddProjectCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		if(!dao.isAdminstrator(user)){
			PMSConsole.println("you are not a adminstrator.");
			return ;
		}
		
		Project project=new Project();
		String code;
		while(true){
			code=PMSConsole.promptNotEmpty("code:");
			if(dao.isProjectExistByCode(code)){
				PMSConsole.println("the code have been existed.");
				continue;
			}
			break; 
		}
		String name=PMSConsole.promptNotEmpty("name:");

		int constructionUnit=PMSConsole.getInt("construction unit id:");
		if(!dao.isCompanyExistById(constructionUnit)){
			PMSConsole.println("the construction unit is not existed.");
			return ;
		}
		
		int firstParty=PMSConsole.getInt("first party id:");
		if(!dao.isCompanyExistById(firstParty)){
			PMSConsole.println("the first party is not existed.");
			return ;
		}
		
		project.setCode(code);
		project.setName(name);
		project.setCreateTime(new Date());
		project.setConstructionUnit(constructionUnit);
		project.setFirstParty(firstParty);
		
		boolean isSuccess=dao.insertProject(project);
		if(isSuccess)
			PMSConsole.println("add project success.");
		else
			PMSConsole.println("add project failed.");
	}

}
